package com.vitabuybuy.tv.ui.auth

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityChangePasswordBinding
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.dto.MessageEvent
import com.library.common.ext.setOnclickNoRepeat
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.auth.viewmodel.AuthViewModel
import com.vitabuybuy.tv.utils.EventCode
import com.vitabuybuy.tv.utils.StringUtil
import kotlinx.android.synthetic.main.activity_change_password.*
import org.greenrobot.eventbus.EventBus

class ChangePasswordActivity : BaseActivity<AuthViewModel, ActivityChangePasswordBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_change_password

    private var from: String? = ""
    private var areaCode: String? = ""
    private var account: String? = ""

    override fun onGetBundle(bundle: Bundle?) {
        from = bundle?.getString("fromAction")
        areaCode = bundle?.getString("areaCode")
        account = bundle?.getString("account")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.change_password))
    }

    override fun eventHandling() {
        setOnclickNoRepeat(ivEye, ivEyeAgain, btnFinish) { v ->
            when (v) {
                ivEye -> {
                    if (etPassword.transformationMethod == HideReturnsTransformationMethod.getInstance()) {
                        etPassword.transformationMethod = PasswordTransformationMethod.getInstance()
                        ivEye.setImageResource(R.drawable.icon_login_eyes_n)
                    } else {
                        etPassword.transformationMethod = HideReturnsTransformationMethod.getInstance()
                        ivEye.setImageResource(R.drawable.icon_login_eyes)
                    }
                }
                ivEyeAgain -> {
                    if (etPasswordAgain.transformationMethod == HideReturnsTransformationMethod.getInstance()) {
                        etPasswordAgain.transformationMethod =
                            PasswordTransformationMethod.getInstance()
                        ivEyeAgain.setImageResource(R.drawable.icon_login_eyes_n)
                    } else {
                        etPasswordAgain.transformationMethod =
                            HideReturnsTransformationMethod.getInstance()
                        ivEyeAgain.setImageResource(R.drawable.icon_login_eyes)
                    }
                }
                btnFinish -> {
                    if (etPassword.text.isEmpty()) {
                        XToast.normal(getString(R.string.enter_password))
                        return@setOnclickNoRepeat
                    }
                    if (etPasswordAgain.text.isEmpty()) {
                        XToast.normal(getString(R.string.password_again))
                        return@setOnclickNoRepeat
                    }
                    if (!StringUtil.judgePassword(etPassword.text.toString()) ||
                        !StringUtil.judgePassword(etPasswordAgain.text.toString())
                    ) {
                        XToast.normal(getString(R.string.password_dialog_tip))
                        return@setOnclickNoRepeat
                    }
                    if (etPassword.text.toString() != etPasswordAgain.text.toString()) {
                        XToast.normal(getString(R.string.twice_password_not_same))
                        return@setOnclickNoRepeat
                    }
                    when (from) {
                        PHONE_RESET -> {
                            viewModel.resetPasswordByPhone(
                                account,
                                areaCode,
                                etPassword.text.toString()
                            )
                        }
                        EMAIL_RESET -> {
                            viewModel.resetPasswordByEmail(account, etPassword.text.toString())
                        }
                    }
                }
            }
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            8 -> {
                XToast.success(getString(R.string.reset_password_success))
                EventBus.getDefault()
                    .post(MessageEvent(EventCode.login_out, getString(R.string.logout)))
            }
        }
    }

    companion object {
        val PHONE_RESET = "phone_reset_pwd"
        val EMAIL_RESET = "email_reset_pwd"
        fun openActivity(
            mContext: BaseActivity<*, *>, account: String, areaCode: String?,
            fromAction: String
        ) {
            val bundle = Bundle()
            bundle.putString("fromAction", fromAction)
            bundle.putString("areaCode", areaCode)
            bundle.putString("account", account)
            mContext.startForResult(bundle, 1001, ChangePasswordActivity::class.java)
        }
    }
}
